<template>
  <div class="header-bar">
    <div class="header-left">
      <BreadCrumbs />
    </div>
    <div class="header-right">
      <div class="current-time">
        <i class="el-icon-time"></i>
        <span>{{ currentTime }}</span>
      </div>
      <div class="header-actions">
        <el-tooltip content="返回前台" placement="bottom">
          <div class="action-btn" @click="goToFront">
            <i class="el-icon-s-home"></i>
            <span>商城首页</span>
          </div>
        </el-tooltip>
        
        <div class="action-group">
          <el-tooltip content="刷新页面" placement="bottom">
            <div class="action-icon-btn" @click="refreshPage">
              <i class="el-icon-refresh-right"></i>
            </div>
          </el-tooltip>
          
          <el-tooltip :content="isFullscreen ? '退出全屏' : '全屏显示'" placement="bottom">
            <div class="action-icon-btn" @click="toggleFullScreen">
              <i :class="isFullscreen ? 'el-icon-close' : 'el-icon-full-screen'"></i>
            </div>
          </el-tooltip>
        </div>

        <div class="divider"></div>
        <UserAvatar />
      </div>
    </div>
  </div>
</template>

<script>
import BreadCrumbs from '../components/BreadCrumbs/index.vue'
import UserAvatar from '../components/UserAvatar/index.vue'

export default {
  name: 'HeaderBar',
  components: {
    BreadCrumbs,
    UserAvatar
  },
  data() {
    return {
      isFullscreen: false,
      currentTime: '',
      timer: null
    }
  },
  methods: {
    refreshPage() {
      window.location.reload()
    },
    toggleFullScreen() {
      if (!document.fullscreenElement) {
        document.documentElement.requestFullscreen()
        this.isFullscreen = true
      } else {
        if (document.exitFullscreen) {
          document.exitFullscreen()
          this.isFullscreen = false
        }
      }
    },
    goToFront() {
      this.$router.push('/');
    },
    updateTime() {
      const now = new Date();
      const options = { 
        year: 'numeric', 
        month: 'long', 
        day: 'numeric', 
        weekday: 'long',
        hour: '2-digit', 
        minute: '2-digit', 
        second: '2-digit' 
      };
      this.currentTime = now.toLocaleDateString('zh-CN', options).replace(/\//g, '-');
    }
  },
  mounted() {
    document.addEventListener('fullscreenchange', () => {
      this.isFullscreen = !!document.fullscreenElement
    })
    this.updateTime();
    this.timer = setInterval(this.updateTime, 1000);
  },
  beforeDestroy() {
    if (this.timer) {
      clearInterval(this.timer);
    }
  }
}
</script>

<style lang="less" scoped>
.header-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
  padding: 0 20px;
  background: linear-gradient(120deg, #ffffff, #fff8e1);
  border-bottom: 1px solid #ffe0b2;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.03);

  .header-left {
    display: flex;
    align-items: center;
  }

  .header-right {
    display: flex;
    align-items: center;
    gap: 18px;

    .current-time {
      display: flex;
      align-items: center;
      gap: 8px;
      color: #757575;
      font-size: 14px;
      background: rgba(255, 248, 225, 0.8);
      padding: 8px 16px;
      border-radius: 24px;
      white-space: nowrap;
      border: 1px solid rgba(255, 152, 0, 0.1);

      i {
        color: #FF9800;
        font-size: 16px;
      }
    }

    .header-actions {
      display: flex;
      align-items: center;
      gap: 14px;

      .action-btn {
        display: flex;
        align-items: center;
        gap: 8px;
        padding: 8px 18px;
        border-radius: 24px;
        cursor: pointer;
        transition: all 0.3s ease;
        background: linear-gradient(135deg, #FF9800, #FFB74D);
        color: white;
        font-size: 14px;
        box-shadow: 0 3px 8px rgba(255, 152, 0, 0.2);

        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 5px 15px rgba(255, 152, 0, 0.3);
        }

        i {
          font-size: 16px;
        }
      }

      .action-group {
        display: flex;
        align-items: center;
        gap: 10px;
        background: rgba(255, 248, 225, 0.8);
        padding: 6px;
        border-radius: 20px;
        border: 1px solid rgba(255, 152, 0, 0.1);
      }

      .action-icon-btn {
        width: 36px;
        height: 36px;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 50%;
        cursor: pointer;
        transition: all 0.3s ease;
        color: #757575;

        i {
          font-size: 18px;
          transition: all 0.3s ease;
        }

        &:hover {
          background-color: white;
          color: #FF9800;
          box-shadow: 0 2px 8px rgba(255, 152, 0, 0.15);

          i.el-icon-refresh-right {
            transform: rotate(180deg);
          }
        }
      }

      .divider {
        width: 1px;
        height: 28px;
        background: linear-gradient(to bottom, transparent, #e0e0e0, transparent);
        margin: 0 8px;
      }
    }
  }
}

:deep(.el-badge__content.is-fixed) {
  top: 8px;
  right: 8px;
  border: 2px solid white;
}

:deep(.el-badge__content) {
  background-color: #FF9800;
  box-shadow: 0 2px 4px rgba(255, 152, 0, 0.2);
}

:deep(.el-tooltip__popper) {
  font-size: 13px;
}

/* 响应式调整 */
@media (max-width: 992px) {
  .current-time span {
    display: none;
  }

  .current-time {
    padding: 8px !important;
  }
}
</style>